﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
using System.IO;

using DaInterFace;

namespace DataApplication
{
    public partial class FormFrpView : Form
    {
        IDataCenter center;
        Thread thread;
        public FormFrpView(IDataCenter center)
        {
            InitializeComponent();
            this.center = center;
        }

        private void FormFrpView_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            thread = new Thread(LoadState);
            thread.IsBackground = true;
            thread.Start();
        }

        private void LoadState()
        {
            while (true)
            {
                System.Diagnostics.Process[] proc = System.Diagnostics.Process.GetProcessesByName("frpc");
                if (proc.Length == 0)
                {
                    TxtProssState.Text = "异常";
                }
                else
                {
                    TxtProssState.Text = "运行中";
                }
                Application.DoEvents();
                try
                {
                    IPHostEntry hostinfo = Dns.GetHostEntry(String.Format("{0}.zzlysbd.com", center.SocketPort));
                    IPAddress[] aryIP = hostinfo.AddressList;

                    if (aryIP.Length > 0)
                    {
                        IPAddress address = aryIP[0];
                        TxtFrpServer.Text = String.Format("一级域名解析正常:*.zzlysbd.com=>{1}", center.SocketPort, address.ToString());
                    }
                }
                catch (Exception ex)
                {
                    TxtFrpServer.Text = ex.Message;
                }

                Application.DoEvents();

                try
                {
                    WebTester.Url = new Uri(String.Format("http://{0}.zzlysbd.com:18701/PDAService.svc/", center.SocketPort));
                    //HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create();
                    //myRequest.Method = "HEAD";　              //设置提交方式可以为＂ｇｅｔ＂，＂ｈｅａｄ＂等
                    //myRequest.Timeout = 10000;　             //设置网页响应时间长度
                    //myRequest.AllowAutoRedirect = false;//是否允许自动重定向
                    //HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                    //if (myResponse.StatusCode == HttpStatusCode.OK)
                    //{
                    //    TxtDns.Text = String.Format("二级域名跳转正常:{0}.zzlysbd.com=>SipServer", center.SocketPort);
                    //}
                    //else
                    //{
                    //    TxtDns.Text = String.Format("二级域名跳转异常:{0}", myResponse.StatusCode.ToString());
                    //}
                }
                catch (Exception ex)
                {
                    TxtDns.Text = ex.Message;
                }

                try
                {
                    System.Net.IPHostEntry localhost = System.Net.Dns.GetHostEntry("radmin.zzlysbd.com");
                    System.Net.IPAddress ip = localhost.AddressList[0];

                    try
                    {
                        IPEndPoint point = new IPEndPoint(ip, 7000);
                        using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                        {
                            sock.Connect(point);

                            sock.Close();

                            TxtRadminServer.Text = "运行正常";
                        }
                    }
                    catch (SocketException e)
                    {
                        TxtRadminServer.Text = "异常:" + e.Message;
                    }

                    try
                    {
                        IPEndPoint point = new IPEndPoint(ip, center.SocketPort - 10000);
                        using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                        {
                            sock.Connect(point);

                            sock.Close();

                            TxtRadminDns.Text = "运行正常:radmin.zzlysbd.com:" + (center.SocketPort - 10000);
                        }
                    }
                    catch (SocketException e)
                    {
                        TxtRadminDns.Text = "异常:" + e.Message;
                    }
                }
                catch (Exception)
                {
                    TxtRadminServer.Text = "网络异常";
                    TxtRadminDns.Text = "网络异常";
                }



                Thread.Sleep(3000);
            }
        }

        private static void KillProcess(string processName) //调用方法，传参
        {
            try
            {
                Process[] thisproc = Process.GetProcessesByName(processName);
                //thisproc.lendth:名字为进程总数

                if (thisproc.Length > 0)
                {
                    for (int i = 0; i < thisproc.Length; i++)
                    {
                        if (!thisproc[i].CloseMainWindow()) //尝试关闭进程 释放资源
                        {
                            thisproc[i].Kill(); //强制关闭
                            thisproc[i].WaitForExit();
                        }
                        Console.WriteLine("进程 {0}关闭成功", processName);
                    }
                }
                else
                {
                    Console.WriteLine("进程 {0} 关闭失败!", processName);
                }
            }
            catch //出现异常，表明 kill 进程失败
            {
                Console.WriteLine("结束进程{0}出错！", processName);
            }
        }

        private void FormFrpView_FormClosing(object sender, FormClosingEventArgs e)
        {
            thread.Abort();
        }

        private void BtnSet_Click(object sender, EventArgs e)
        {
            center.ProxyReset();
        }

        private void WebTester_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (WebTester.DocumentTitle == "PDAApi 服务")
            {
                TxtDns.Text = String.Format("二级域名跳转正常:{0}.zzlysbd.com=>SipServer(1.0)", center.SocketPort);
            }
            else
            {
                TxtDns.Text = String.Format("二级域名跳转异常", center.SocketPort);
            }
        }
    }
}
